﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace AffichageGrille
{
    class Level
    {
        public int[,] Grille { get; set; }

        Texture2D textureCase;
        Texture2D textureCaseInfranchissable;
        SpriteFont coordonneesFont;

        public int X { get; set; }
        public int Y { get; set; }

        public Level(string fichier,ContentManager content, int x, int y)
        {
            X = x;
            Y = y;

            textureCase = content.Load<Texture2D>("case");
            textureCaseInfranchissable = content.Load<Texture2D>("caseInfranchissable");
            coordonneesFont = content.Load<SpriteFont>("coordonneesFont");

            Grille = new int[Config.LargeurGrille, Config.HauteurGrille];

            StreamReader reader = new StreamReader(fichier);

            int numeroligne = 0;

            while (!reader.EndOfStream)
            {
                string ligne = reader.ReadLine();

                for (int i = 0; i < Config.LargeurGrille; i++)
                {
                    Grille[i, numeroligne] = int.Parse(ligne[i].ToString());
                }

                numeroligne++;
            }

            reader.Close();
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < Config.LargeurGrille; i++)
            {
                for (int j = 0; j < Config.HauteurGrille; j++)
                {
                    switch (Grille[i, j])
                    {
                        case 1:
                            spriteBatch.Draw(textureCase, new Vector2(i * 40, j * 40), Color.White);
                            break;
                        case 0:
                            spriteBatch.Draw(textureCaseInfranchissable, new Vector2(i * 40, j * 40), Color.White);
                            break;
                    }
                }
            }

            spriteBatch.DrawString(coordonneesFont, X.ToString() + " | " + Y.ToString(), new Vector2(1, 1), Color.Red);
        }
    }
}
